<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author zwang
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class user extends User_Controller {

    //put your code here
    function __construct() {
        parent::__construct();

        $username = $this->session->userdata('username');
        $current_userType = $this->get_userType($username);
        if ($current_userType == $this->type_user || $current_userType == $this->type_admin) {
            $this->load->model("database");
        } else {
            $this->redirect($current_userType);
        }
    }

    function index() {
        if ($this->session->userdata('validated')) {
            $username = $this->session->userdata('username');
            $current_userType = $this->get_userType($username);
            if ($current_userType != $this->type_user) {
                $this->redirect($current_userType);
            }
            $username = $this->session->userdata('username');
            $userid = $this->get_userID($username);
            $user_profile = $this->get_userProfile($userid);
            $data = array(
                'userid' => $userid,
                'username' => $username,
                'email' => $user_profile->email,
                'base_url' => base_url()
            );

            $captcha = $this->get_captcha();

            $this->load->view('user/userPage', array('data' => $data, 'captcha' => $captcha));
        } else {
            redirect('login', 'refresh');
        }
    }

    function showProfile() {
        $username = $this->session->userdata('username');
        $userid = $this->get_userID($username);
        $user_profile = $this->get_userProfile($userid);
        $data = array(
            'userid' => $userid,
            'username' => $username,
            'firstName' => $user_profile->firstName,
            'lastName' => $user_profile->lastName,
            'email' => $user_profile->email,
        );
        $this->load->view("user/profile_view", $data);
    }

    function saveProfile() {
        $userid = $this->input->get_post('userid');
        $firstName = $this->input->get_post('firstName');
        $lastName = $this->input->get_post('lastName');
        $email = $this->input->get_post('email');
        if ($this->database->updateProfile($userid, $firstName, $lastName, $email)) {
            echo "success";
            return true;
        }
        return false;
    }

    function changePassword() {
        if ($this->session->userdata('is_logged_in')) {
            $this->load->view('change_password');
            return;
        }
        redirect('citportal');
    }

    function savePassword() {
        $this->form_validation->set_rules('cur_pw', 'Old Password', 'required|trim|xss_clean|callback_verifyPassword');
        $this->form_validation->set_rules('new_pw', 'New Password', 'trim|xss_clean|required|matches[con_pw]|min_length[7]|md5');
        $this->form_validation->set_rules('con_pw', 'Confirm Password', 'required|xss_clean|trim');
        $this->form_validation->set_error_delimiters('<p style="color: red; font-size: 11px">', '</p>');

        if ($this->form_validation->run() == FALSE) {
            $error = 'There were problems changing your password.';
            $this->load->view('change_password', array('error' => $error));
            return false;
        } else {
            $new_pw = $this->input->get_post('con_pw');
            if ($this->database->changePassword($this->session->userdata('userid'), $new_pw)) {
                $success = "You've successfully changed your password";
                $this->load->view('change_password', array('success' => $success));
            } else {
                $error = 'You failed to change your password.';
                $this->load->view('change_password', array('error' => $error));
            }
        }
    }

    function verifyPassword($cur_pw) {
        if (!$this->database->verifyPassword($this->session->userdata('userid'), $cur_pw)) {
            $this->form_validation->set_message('verifyPassword', "Password is incorrect, please enter again. ");
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function postQuestion() {
        $username = $this->session->userdata('username');
        $userid = $this->get_userID($username);
        $user_profile = $this->get_userProfile($userid);
        $data = array(
            'userid' => $userid,
            'username' => $username,
            'email' => $user_profile->email,
            'base_url' => base_url()
        );

        $captcha = $this->get_captcha();
        $this->form_validation->set_rules('title', 'title', 'trim|strip_tags|xss_clean|required|max_length[128]');
        $this->form_validation->set_rules('affiliation', 'affiliation', 'trim|strip_tags|xss_clean|required|max_length[64]');
        $this->form_validation->set_rules('category', 'affiliation', 'trim|strip_tags|xss_clean|required');
        $this->form_validation->set_rules('email', 'email', 'trim|strip_tags|xss_clean|required|valid_email');
        $this->form_validation->set_rules('question', 'question', 'trim|strip_tags|xss_clean|required');
        $this->form_validation->set_rules('captcha', 'captcha', 'trim|strip_tags|xss_clean|callback_captcha_check|match_captcha[captcha.word]');
        $this->form_validation->set_error_delimiters('<p class="disclaimer" style="text-align: left; color: red; font-size: 13px">', '</p>');
        if ($this->form_validation->run() == false) {
            // Hasn't run or thera are some validation errors
            $this->session->set_flashdata('error', 'Question failed to post!');
//                redirect('user', array('data' => $data, 'captcha' => $captcha));
            $this->load->view('user/userPage', array('data' => $data, 'captcha' => $captcha));
            return;
        } else {
            $username = $this->get_username();
            $title = $this->input->post('title');
            $userid = $this->get_userID($username);
            $affiliation = $this->input->post('affiliation');
            $email = $this->input->post('email');
            $category = $this->input->post('category');
            $question = $this->input->post('question');
            if ($this->database->insertQuestion($userid, $title, $affiliation, $email, $category, $question)) {
                $this->session->set_flashdata('message', 'Question has been posted!');
            } else {
                $this->session->set_flashdata('error', 'Question failed to post!');
            }

            // Everything is go - process the form 
        }
        redirect('user', array('data' => $data, 'captcha' => $captcha));
    }

    function captcha_check($value) {
        if ($value == '') {
            $this->form_validation->set_message('captcha_check', 'Please enter the text from the image left, and submit you question.');
            return false;
        } else {
            return true;
        }
    }

    function editPost() {
//        $captcha = $this->get_captcha();
        $id = $this->input->get_post('id');
        $post = $this->database->getPostByID($id);
//        $userid = $post['userid'];
//        $name = $this->get_userFullName($userid);
//        $ans = $this->database->getAnswer($id)->result_array();
//        $ans_num = $this->database->getAnswer($id)->num_rows();
        $this->load->view('user/edit_post', array('post' => $post));
    }

    function savePost() {
        $id = $this->input->get_post('id');
        $title = $this->input->get_post('title');
        $affiliation = $this->input->get_post('affiliation');
        $email = $this->input->get_post('email');
        $category = $this->input->get_post('category');
        $question = $this->input->get_post('question');
        if ($this->database->savePost($id, $title, $category, $affiliation, $email, $question)) {
            echo "Success";
            return true;
        }
        return false;
    }

    function deletePost() {
        $id = $this->input->get_post('id');
        if ($this->database->deletePost($id)) {
            echo "success";
            return true;
        }
        return false;
    }

    function logout() {
        $this->session->unset_userdata('validated');
        $this->session->unset_userdata('is_logged_in');
        session_unset();
        session_destroy();
        redirect('citportal');
    }

}

?>
